package com.google.code.hibernate.rest.example;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * 
 * @author wangzijian
 * 
 */
@Entity
@Table
public class Student extends Person {

	private Information information;

	@OneToMany(mappedBy = "student", cascade = CascadeType.REMOVE)
	private List<Book> books;
	
	@XStreamOmitField
	@OneToMany(mappedBy = "student", cascade = CascadeType.REMOVE)
	private List<StudentTeacher> studentTeachers;

	public Student() {

	}

	public Student(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", information=" + information + "]";
	}

	public void addBook(Book book) {
		getBooks().add(book);
		book.setStudent(this);
	}

	public Information getInformation() {
		return information;
	}

	public void setInformation(Information information) {
		this.information = information;
	}

	public List<StudentTeacher> getStudentTeachers() {
		if (studentTeachers == null) {
			studentTeachers = new ArrayList<StudentTeacher>();
		}
		return studentTeachers;
	}

	public void setStudentTeachers(List<StudentTeacher> studentTeachers) {
		this.studentTeachers = studentTeachers;
	}

	public List<Book> getBooks() {
		if (books == null) {
			books = new ArrayList<Book>();
		}
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((information == null) ? 0 : information.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (information == null) {
			if (other.information != null)
				return false;
		} else if (!information.equals(other.information))
			return false;
		return true;
	}

}
